package com.ton.utils;

public class CastUtil {
    public static Integer castInt(Object obj) {
        return castInt(obj, 0);
    }

    public static Integer castInt(Object obj, Integer defaultValue) {
        return cast(obj, Integer.class, defaultValue);
    }

    public static Double castDouble(Object obj) {
        return castDouble(obj, 0D);
    }

    public static Double castDouble(Object obj, Double defaultValue) {
        return cast(obj, Double.class, defaultValue);
    }

    public static Long castLong(Object obj) {
        return castLong(obj, 0L);
    }

    public static Long castLong(Object obj, Long defaultValue) {
        return cast(obj, Long.class, defaultValue);
    }

    public static Float castFloat(Object obj) {
        return castFloat(obj, 0f);
    }

    public static Float castFloat(Object obj, Float defaultValue) {
        return cast(obj, Float.class, defaultValue);
    }

    public static String castString(Object obj) {
        return castString(obj, "");
    }

    public static String castString(Object obj, String defaultValue) {
        return cast(obj, String.class, defaultValue);
    }


    public static <T> T cast(Object obj, Class<T> clazz, Object defaultValue) {
        if (clazz == Integer.class || clazz == int.class) {
            if (obj == null) {
                return (T) defaultValue;
            }
            if (obj instanceof Integer) {
                return (T) obj;
            }
            return (T) Integer.valueOf(obj.toString());
        }
        if (clazz == Long.class || clazz == long.class) {
            if (obj == null) {
                return (T) defaultValue;
            }
            if (obj instanceof Long) {
                return (T) obj;
            }
            return (T) Long.valueOf(obj.toString());
        }
        if (clazz == Double.class || clazz == double.class) {
            if (obj == null) {
                return (T) defaultValue;
            }
            if (obj instanceof Double) {
                return (T) obj;
            }
            return (T) Double.valueOf(obj.toString());
        }
        if (clazz == Float.class || clazz == float.class) {
            if (obj == null) {
                return (T) defaultValue;
            }
            if (obj instanceof Float) {
                return (T) obj;
            }
            return (T) Float.valueOf(obj.toString());
        }

        if (clazz == String.class) {
            if (obj == null || "".equals(obj)) {
                return (T) defaultValue;
            }
            if (obj instanceof String) {
                return (T) obj;
            } else {
                return (T) obj.toString();
            }
        }
        if (obj == null) {
            return (T) defaultValue;
        }
        return (T) obj;
    }

    public static boolean isBasicType(Class clazz) {
        return clazz == int.class || clazz == Integer.class ||
                clazz == long.class || clazz == Long.class ||
                clazz == double.class || clazz == Double.class ||
                clazz == float.class || clazz == Float.class ||
                clazz == String.class;
    }
}
